package com.saumya.utils 
{
	import com.saumya.robotlegs.events.LoggerEvent;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.filesystem.File;
	import flash.utils.Timer;

	/**
	 * Divides files and folders in 2 different arrays from inside a particular folder
	 * 
	 * @author saumya
	 * @version 0.1
	 */
	public class FileSearchUtil extends EventDispatcher
	{
		private var searchingInFolder:File = null;
		private var totalItemsInFolder:Array = [];
		private var totalItemCountsInFolder:uint = 0;
		
		private var allFiles:Array = [];
		private var allFolders:Array = [];
		
		public static const SEARCH_COMPLETE:String = 'searchCompleteEvent';
		
		private var fileTypeToSearch:String = '';
		
		public function FileSearchUtil(searchType:String='') 
		{
			//
			this.fileTypeToSearch = searchType;
		}
		
		//public function searchInFolder(folder:File,parentRef:Main):void
		public function searchInFolder(folder:File):void
		{
			this.searchingInFolder = folder;
			this.totalItemsInFolder = this.searchingInFolder.getDirectoryListing();
			this.totalItemCountsInFolder = this.totalItemsInFolder.length;
			this.allFiles = [];
			this.allFolders = [];
			
			this.startSearch();
		}
		
		private function startSearch():void
		{
			var t:Timer = new Timer(0, totalItemCountsInFolder);
			t.addEventListener(TimerEvent.TIMER, onTime);
			t.addEventListener(TimerEvent.TIMER_COMPLETE, onTimeComplete);
			t.start();
		}
		
		private function onTimeComplete(e:TimerEvent):void 
		{
			var t:Timer = Timer(e.target);
			t.removeEventListener(TimerEvent.TIMER, onTime);
			t.removeEventListener(TimerEvent.TIMER_COMPLETE, onTimeComplete);
			t = null;
			
			var msg:String = '::: Search is over ::: files='+this.allFiles.length+' : folders='+this.allFolders.length;
			//
			var evt:Event = new Event(FileSearchUtil.SEARCH_COMPLETE);
			this.dispatchEvent(evt);
		}
		
		private function onTime(e:Event):void 
		{
			var t:Timer = Timer(e.target);
			var i:int = t.currentCount;
			var f:File = this.totalItemsInFolder[i-1];
			if (f.isDirectory)
			{
				this.allFolders.push(f);
				//
				this.searchForFilesInFolder(f);
			}else {
				if (this.fileTypeToSearch=='')
				{
					this.allFiles.push(f);
				}else {
					var ext:String = f.extension;
					if (this.fileTypeToSearch==ext)
					{
						this.allFiles.push(f);
					}
				}
				
			}
			
			var msg:String = t.currentCount + ' : '+f.name+' : folders='+this.allFolders.length+' : files='+this.allFiles.length;
			//logger event to display on the log
			var le:LoggerEvent=new LoggerEvent(LoggerEvent.UPDATE_LOG);
			le.setLogMessage(msg);
			this.dispatchEvent(le);
			
		}
		
		private function searchForFilesInFolder(folder:File):void
		{
			var msg:String = 'now searching in Folder='+folder.name;
			
			var allItems:Array = folder.getDirectoryListing();
			for (var ui:uint = 0; ui < allItems.length;ui++ )
			{
				var f:File = allItems[ui];
				if (f.isDirectory)
				{
					if (f.name=='.svn')
					{
						//DO Nothing
					}else {
						searchForFilesInFolder(f);
					}
					
				}else {
					if (this.fileTypeToSearch=='')
					{
						this.allFiles.push(f);
					}else {
						var ext:String = f.extension;
						if (this.fileTypeToSearch==ext)
						{
							this.allFiles.push(f);
						}
					}
				}
			}
		}
		
		public function getMeFiles():Array
		{
			return this.allFiles;
		}
		
		public function getMeFolders():Array
		{
			return this.allFolders;
		}
		
		public function getFileTypeSearched():String
		{
			return this.fileTypeToSearch;
		}
		
		
	}

}